package engine.managers
{
	import ash.core.Engine;
	import ash.core.Entity;
	import ash.core.NodeList;
	import ash.core.System;
	import ash.signals.Signal0;
	import ash.tick.FrameTickProvider;
	
	import engine.GameEngine;
	import engine.groups.Group;
	import engine.groups.Scene;
	import engine.systems.group.Children;
	import engine.systems.group.Parent;
	import engine.utils.ClassUtils;
	import engine.utils.Command;
	
	import flash.display.DisplayObject;
	import flash.utils.Dictionary;
	
	import org.as3commons.collections.LinkedSet;

	public class GroupManager
	{
		private var _scene:Scene;
		private var _groups:Dictionary = new Dictionary();
		
		public function GroupManager()
		{
			
		}
		
		public final function get scene():Scene { return this._scene; }
		public final function get groups():Vector.<Group>
		{
			var groups:Vector.<Group> = new Vector.<Group>();
			for each(var group:Group in this._groups)
			{
				groups.push(group);
			}
			return groups;
		}
		
		public final function setScene(sceneClass:Class):void
		{	
			this.removeScene();
			
			this._scene = ClassUtils.construct(sceneClass, [GameEngine.screenManager.sceneContainer, null]);
			this.add(this._scene);
		}
		
		public final function removeScene():Boolean
		{
			if(!this._scene) return false;
			
			return this.remove(this._scene);
		}
		
		public final function getByName(name:String):Group
		{
			if(!name) return null;
			
			return this._groups[name];
		}
		
		public final function getByClass(groupClass:Class):Group
		{
			if(!groupClass) return null;
			
			for each(var group:Group in this._groups)
			{
				if(group is groupClass) return group;
			}
			
			return null;
		}
		
		public final function add(group:Group):Boolean
		{
			if(this._groups[group.name]) return false;
			
			this._groups[group.name] = group;
			
			return true;
		}
		
		public final function remove(group:Group):Boolean
		{
			if(!group) return false;
			
			if(!this._groups[group.name]) return false;
			
			delete _groups[group.name];
			group.destroy();
			
			return true;
		}
		/*
		public static function getNodeList(nodeClass:Class):NodeList
		{
			var nodeList:NodeList = _engine.getNodeList(nodeClass);
			if(nodeList.empty)
			{
				_engine.releaseNodeList(nodeClass);
				return null;
			}
			return nodeList;
		}*/
	}
}